package com.cbgolf.oa.model;

import android.app.Activity;
import android.content.Intent;
import com.cbgolf.oa.base.BaseModel;
import com.cbgolf.oa.entity.AccountOrderBean;
import com.cbgolf.oa.entity.BillUserBean;
import com.cbgolf.oa.net.NetCallBack;
import com.cbgolf.oa.net.NetUtils;
import com.cbgolf.oa.net.Web;
import com.cbgolf.oa.net.WebAPI;
import com.cbgolf.oa.presenter.IOrderPresenter;
import com.cbgolf.oa.util.DataUtil;
import com.cbgolf.oa.util.ErrorUtil;
import com.cbgolf.oa.util.TextUtil;
import com.cbgolf.oa.util.Util;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Created by lq on 2017/11/4.
 */

public class OrderModel extends BaseModel {

  private IOrderPresenter worker;

  private String customerId;
  private String teeTimeDate;

  private int page = 0;
  private int pageSize = 15;

  private final int LOAD_INIT = 0;
  private final int REFRESH = 1;
  private final int LOADMORE = 2;
  private final int CHOOSE_DATE = 3;
  private final int FILTER_USER = 4;

  public OrderModel(Activity act, IOrderPresenter worker) {
    this.worker = worker;
    super.init(act);
  }

  @Override
  protected void getInitData() {
    Intent intent = context.getIntent();
    customerId = DataUtil.getStringExtra(intent, "customerId");
    getData(LOAD_INIT);
    getUsers();
  }

  private void getUsers() {
    Web.getOK(WebAPI.billuser_list_get, new NetCallBack(response -> {
      if (NetUtils.SUCCESS != response.statusCode) {
        worker.getUserListFail(response.errorMsg);
        return;
      }
      List<BillUserBean> list = getList(response, BillUserBean.class);
      worker.getUserlistSuccess(list);
    }));
  }

  public void setTeeTimeDate(String time) {
    this.teeTimeDate = time;
  }

  public void setCustomerId(String id) {
    this.customerId = id;

  }

  public void getData(final int type) {
    String url = WebAPI.order_list_get;
    Map<String, String> map = new HashMap<>();
    map.put("signingId", TextUtil.Text(customerId));
    map.put("teeTimeDate", Util.isNull(teeTimeDate) ? String.valueOf(System.currentTimeMillis())
        : TextUtil.Text(teeTimeDate));
    map.put("pageSize", TextUtil.textNumInt(pageSize));
    map.put("currentPage", (type == LOADMORE ? TextUtil.textNumInt(++page) : "0"));
    map.put("token", DataUtil.getToken());
    worker.showProgress();
    Web.getOK(url, map, new NetCallBack(response -> {
      worker.stopProgress();
      if (NetUtils.SUCCESS != response.statusCode) {
        setFail(type, response.errorMsg);
        return;
      }
      setRequestSuccess(type, getList(response, AccountOrderBean.class));
    }));
  }

  private void setRequestSuccess(int type, List<AccountOrderBean> list) {
    switch (type) {
      case LOAD_INIT:
        worker.getDataSuccess(list);
        break;
      case LOADMORE:
        worker.loadMoreSuccess(list);
        break;
      case REFRESH:
        worker.refershSuccess(list);
        break;
      case CHOOSE_DATE:
        worker.getDataByDateSuccess(list);
        break;
      case FILTER_USER:
        worker.getDataByUserSuccess(list);
        break;
      default:
        break;
    }

  }

  private void setFail(int type, String msg) {
    switch (type) {
      case LOAD_INIT:
        worker.getDataFail(msg);
        break;
      case LOADMORE:
        reducePage();
        worker.loadMoreFail(msg, ErrorUtil.NETERROR_CODE);
        break;
      case REFRESH:
        worker.refershFail(msg, ErrorUtil.NETERROR_CODE);
        break;
      case CHOOSE_DATE:
        worker.getDataByDateFail(msg);
        break;
      case FILTER_USER:
        worker.getDataByUserFail(msg);
        break;
      default:
        break;
    }
  }

  private void reducePage() {
    page = ((page > 0) ? --page : 0);
  }
}
